//
//  PeopleProjectCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-8.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "PeopleProjectCheckViewController.h"
#import "PeopleProjectCell.h"
@interface PeopleProjectCheckViewController ()
{
    NSMutableArray *isNormals;
    NSMutableArray *results;
    NSMutableArray *remarks;
}
@end

@implementation PeopleProjectCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.checkURL = resultOfPeopleProject;
    self.checkNames = @[@"点动监测情况",@"处理情况",@"备注",@""];
    [super viewDidLoad];
    self.title = @"人防工程巡检查询";
    isNormals = [NSMutableArray array];
    results = [NSMutableArray array];
    remarks = [NSMutableArray array];
	// Do any additional setup after loading the view.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    PeopleProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[PeopleProjectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        if (self.floorCheckDatas.count > 0)
        {
            NSArray *checkDatas = [self.floorCheckDatas objectAtIndex:indexPath.row];
            for (int i = 0; i < checkDatas.count; i ++)
            {
                NSDictionary *checkDic = [checkDatas objectAtIndex:i];
                NSString *checkResult = [NSString stringWithFormat:@"%@",[checkDic objectForKey:@"result"]];
                UIControl *control = [UIControl new];
                CGSize mySz;
                if ([checkResult isEqualToString:@"0"])
                {
                    UIImage *rightImage = [UIImage imageNamed:@"正常"];
                    mySz = rightImage.size;
                    UIImageView *rightView = [[UIImageView alloc]initWithImage:rightImage];
                    control = (UIControl*)rightView;
                }
                else
                {
                    UILabel *resultLb = [UILabel new];
                    resultLb.text = checkResult;
                    resultLb.font = [UIFont systemFontOfSize:20];
                    mySz = [resultLb.text sizeWithFont:resultLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
                    control = (UIControl*)resultLb;
                }
                control.frame = CGRectMake(cell.floorNameLb.frame.origin.x + cell.floorNameLb.frame.size.width + i * (900/(checkDatas.count + 1)),cell.floorNameLb.frame.origin.y, mySz.width, mySz.height);
                [cell addSubview:control];
            }
            
        }
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.floorNames.count > 0) {
        cell.floorNameLb.text = [self.floorNames objectAtIndex:indexPath.row];
    }
    if (remarks > 0 && indexPath.row <= remarks.count)
    {
        cell.remarkLb.text = [remarks objectAtIndex:indexPath.row];
    }
    if (results.count > 0) {
        cell.resultLb.text = [results objectAtIndex:indexPath.row];
    }
    if (isNormals.count > 0) {
        cell.isNormalLb.text = [isNormals objectAtIndex:indexPath.row];
    }
    return cell;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    NSLog(@"查询%@",floorData);
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"floorName"])
    {
        self.floorNames = [NSMutableArray array];
    }
    
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [self.floorNames addObject:[queryDic objectForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"remark"]) {
            [self.floorRemarks addObject:[queryDic objectForKey:@"remark"]];
            [remarks addObject:[queryDic objectForKey:@"remark"]];
        }
        if ([queryDic objectForKey:@"isNormal"]) {
            [isNormals addObject:[queryDic objectForKey:@"isNormal"]];
        }
        if ([queryDic objectForKey:@"result"]) {
            [results addObject:[queryDic objectForKey:@"result"]];
        }
    }
    [self.mainTable reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
